using System.Linq;
using UnityEngine;

namespace Voodoo.Sauce.Internal.Analytics
{
	public static class AbTestHelper
	{
		private const string PrefCohort = "VoodooSauce_Cohort";

		private const string CohortDefault = "Control";

		public const int ControlCohortCount = 1;

		public const string ABTestDefaultCountryCode = "us";

		internal static string GenerateNewRandomCohort(string[] runningAbTests, float usersPercentPerCohort)
		{
			if (runningAbTests == null || runningAbTests.Length == 0)
			{
				return null;
			}
			float randomValue = Random.value;
			return runningAbTests.Where((string abTest, int index) => randomValue < (float)(index + 1) * usersPercentPerCohort).FirstOrDefault();
		}

		internal static string[] AddControlCohortsToAbTests(string[] runningAbTests)
		{
			string[] array = new string[runningAbTests.Length + 1];
			for (int i = 0; i < 1; i++)
			{
				array[i] = "Control " + (i + 1);
			}
			for (int j = 0; j < runningAbTests.Length; j++)
			{
				array[j + 1] = runningAbTests[j];
			}
			return array;
		}

		internal static bool HasSavedPlayerCohort()
		{
			return NintendoPlayerPrefs.HasKey("VoodooSauce_Cohort");
		}

		internal static string GetSavedPlayerCohort()
		{
			return NintendoPlayerPrefs.GetString("VoodooSauce_Cohort", null);
		}

		public static void SavePlayerCohort(string cohort)
		{
			if (cohort == null)
			{
				if (NintendoPlayerPrefs.HasKey("VoodooSauce_Cohort"))
				{
					NintendoPlayerPrefs.DeleteKey("VoodooSauce_Cohort");
				}
			}
			else
			{
				NintendoPlayerPrefs.SetString("VoodooSauce_Cohort", cohort);
			}
		}
	}
}
